<?php

namespace plugin\muadmin\app\common\model;

use think\Model;

class AuthRule extends Model
{
    protected $name = 'auth_rule';
    protected $autoWriteTimestamp = 'datetime';
    protected $pk = 'id';
    protected $createTime  = 'createtime';
    protected $updateTime  = 'updatetime';
    public static function  onAfterInsert($model)
    {
        if(empty($model->weigh)){
            $model->save(['weigh'=>$model->id]);
        }
    }
    public function getAllIds(string|array|int $pids=0)
    {
        if(is_string($pids)){
            $pids = explode(',',$pids);
        }
        if(is_numeric($pids)){
            $pids = [$pids];
        }
        $child_ids = $this->whereIn('pid',$pids)->column('id');
        if(!empty($child_ids)){
            $newchilds = $this->getAllIds($child_ids);
            if(!empty($newchilds)){
                $child_ids = array_merge($child_ids,$newchilds);
            }
        }
        $child_ids = array_merge($child_ids,$pids);
        $child_ids = array_filter($child_ids);
        $child_ids  = array_unique($child_ids);
        sort($child_ids);
        return  $child_ids;
    }
}